package com.hissy.common.algorithm;

import com.alibaba.fastjson.JSON;

import org.junit.Test;

/**
 * Created by zhangyanfei on 2018/10/31.
 * 算法 最有输入输出解决方案
 * rank
 * 数据结构、分治策略
 *
 * @author zhangyanfei
 * @date 2018/10/31
 */
public class daily_practice_01 {
    /**
     * x,y,z
     *
     * a,b,c
     *
     * a!=x c!=x,z
     */
    @Test
    public void test_c1() {
        String[] A = {"a", "b", "c"};
        String[] B = {"x", "y", "z"};
        String result = "";
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 3; k++) {
                    String str;
                    str = A[i] + A[j] + A[k];
                    if (str.contains("a") && str.contains("b") && str.contains("c")) {
                        if (str.indexOf("a") != 0 && str.indexOf("c") != 0 && str.indexOf("c") != 2) {
                            result = str;
                            break;
                        }
                    }
                }
            }
        }

        System.out.println(result);
    }

    private int[] array = {6,5,4,3,2,1};

    /**
     * 比较排序
     */
    @Test
    public void test_c2() {
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] > array[j]) {
                    array[i] = array[i] + array[j];
                    array[j] = array[i] - array[j];
                    array[i] = array[i] - array[j];
                }
            }
            for (int k = 0; k < array.length; k++) {
                System.out.print(array[k] + ",");
            }
            System.out.println();
        }

    }

    /**
     * 冒泡排序
     * 两两比较
     */
    @Test
    public void test_c3() {
        for (int i = 0; i < array.length - 1; i++) {
            // 冒泡一次就少一个
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] < array[j + 1]) {
                    array[j] = array[j] + array[j + 1];
                    array[j + 1] = array[j] - array[j + 1];
                    array[j] = array[j] - array[j + 1];
                }
            }
            for (int k = 0; k < array.length; k++) {
                System.out.print(array[k] + ",");
            }
            System.out.println();
        }
    }

    /**
     *
     *
     */
    @Test
    public void test_c4() {
        int length = array.length;
        int gap = length / 2;
        System.out.println(JSON.toJSONString(array));
        while (gap != 0) {
            for (int i = gap; i < length; i++) {
                //
                for (int j = i - gap; j >= 0; j -= gap) {
                    if (array[j] > array[j + gap]) {
                        array[j] = array[j] + array[j + gap];
                        array[j + gap] = array[j] - array[j + gap];
                        array[j] = array[j] - array[j + gap];
                    }
                    for (int k = 0; k < array.length; k++) {
                        System.out.print(array[k] + ",");
                    }
                    System.out.println();
                }
            }
            System.out.println("result:----");
            for (int k = 0; k < array.length; k++) {
                System.out.print(array[k] + ",");
            }
            System.out.println();
            gap = gap / 2;
        }
    }
}
